<template>
  <j-modal
    :title="title"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所属模块">
          <cn-dict-select-tag  v-decorator="['module', validatorRules.module]" :triggerChange="true" placeholder="请选择所属模块"
                              dictCode="module"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input placeholder="请输入接口名称" v-decorator="['impName', validatorRules.impName]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标记">
          <a-input placeholder="请输入接口标记" v-decorator="['impKey', validatorRules.impKey]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="bean">
          <a-input placeholder="请输入接口bean" v-decorator="['impBean', validatorRules.impBean]" />
        </a-form-item>
        <a-form-item label="优先级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <cn-dict-select-tag
            placeholder="请选择级别"
            dict-code="priority"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'priority', validatorRules.priority]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否启用/停用">
          <a-radio-group v-decorator="['status',{...validatorRules.status,initialValue:1}]" placeholder="是否启用/停用">
            <a-radio :value="1">
              启用
            </a-radio>
            <a-radio :value="0">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="启用/停用">-->
<!--          <cn-dict-select-tag type="radioButton" v-decorator="[ 'status', {'initialValue':1}]" :trigger-change="true" dictCode="user_status"/>-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remarks', {}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import AFormItem from 'ant-design-vue/es/form/FormItem'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JImageUpload from '@/components/cn-form/JImageUpload'
  import JTreeSelect from '@/components/cn-form/JTreeSelect'
  import JSelectUserByDep from '@/components/cn-select-popup/JSelectUserByDep'
  export default {
    name: 'SfaPicFactoryModal',
    components: {
      JImageUpload,
      AFormItem,
      ATextarea,
      JTreeSelect,
      JSelectUserByDep
    },
    data () {
      return {
        title:"操作",
        visible: false,
        selecttype:'',
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          module:{ rules: [{
              required: true, message: ''
            }]},
          impName:{ rules: [{
              required: true, message: ''
            }]},
          impKey:{ rules: [{
              required: true, message: ''
            }]},
          impBean:{ rules: [{
              required: true, message: ''
            }]},
          priority:{ rules: [{
              required: true, message: ''
            }]},
          status: {
            rules: [{
              required: true, message: ''
            }]
          }
        },
        url: {
          add: window._CONFIG['domianJobURL']+ "/downConfig/saveOrUpdate",
        },
      }
    },
    methods: {
      dotype(val){
        this.selecttype = val
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.isRoll = this.model.isRoll+"";
        this.model.isControl = this.model.isControl+"";
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'impName','impKey','impBean','module',
            'priority','remarks','status'))
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            httpurl += this.url.add;
            let method = '';
            method = 'post';
            let formData = Object.assign(this.model, values);
            //时间格式化
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>
